package sql;
import java.sql.*;

public class SqlExecutor {
	static String user = "dev";
	static String password = "develop";
	static String url = "jdbc:mysql://localhost:3306/gentoo";
	static String driver = "com.mysql.jdbc.Driver";
	static Connection conn=null;
	static{
		init();
	}
	
	public static void init(){
		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);
		}
		catch(Exception ex){
			ex.printStackTrace();
			System.exit(0);
		}
	}
	public static void dispose(){
		try {
			if(!conn.isClosed()){
				conn.close();
				conn = null;
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static ResultSet executeQuery(String sql){
		Statement stmt=null;
		ResultSet rs=null;
		try {
			System.out.println("execute: "+sql);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			//stmt.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public static int executeUpdate(String sql){
		Statement stmt=null;
		int count=0;
		try {
			stmt = conn.createStatement();
			count = stmt.executeUpdate(sql);
		}
		catch (SQLException e) {
			e.printStackTrace();			
		}
		return count;
	}
	
	public static void closeResultSet(ResultSet rs){
		try {
			rs.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
